#define DEBUG
#include <cstdio>

using namespace std;

typedef long long ll;

const int maxn=2500, mods=1000000007;

ll c[maxn+1][maxn+1], pow[maxn+1][maxn+1];

void initC(int n) {
  c[0][0] = 1;
  for (int i=1; i<=n; i++) {
    c[i][0] = 1;
    for (int j=1; j<=n; j++) {
      c[i][j] = (c[i-1][j-1]+c[i-1][j])%mods;
    }
  }
}

void initPow(int n) {
  for (int i=1; i<=n; i++) {
    pow[i][0] = 1;
    for (int j=1; j<=n; j++) {
      pow[i][j] = pow[i][j-1]*i%mods;
    }
  }
}

class Tree {
public:
  int n, m, ind[maxn+1], to[2*maxn-1], val[2*maxn-1], link[2*maxn-1], size[maxn+1];
  
  void addSide(int u, int v, int w) {
    m++;
    to[m] = v;
    val[m] = w;
    link[m] = ind[u];
    ind[u] = m;
  }

  ll build(int o, int fa, int m) {
    static ll sum[3][maxn+1];
    ll ret=0;
    size[o]=1;
    for (int i=ind[o]; i; i=link[i]) {
      if (to[i]!=fa) {
	ret = (ret+build(to[i], o, m))%mods;
	int an=size[to[i]];
	size[o] += an;
	sum[0][0] = pow[n-an][m];
	sum[2][0] = sum[0][0]*2*m%mods;
	for (int j=1; j<=m; j++) {
	  ll t=c[m][j]*pow[an][j]%mods*pow[n-an][m-j]%mods;
	  sum[0][j] = (sum[0][j-1]+t)%mods;
	  sum[1][j] = (sum[1][j-1]+t*j%mods)%mods;
	  sum[2][j] = (sum[2][j-1]+t*(2*m-j)%mods)%mods;
	}
	for (int j=0;j<=m; j++) {
	  ll t=val[i]*c[m][j]%mods
	    *pow[an][j]%mods*pow[n-an][m-j]%mods,
	    tx, ty;
	  tx=sum[1][m-j], ty=sum[0][m-j];
	  ret = (ret+(tx+ty*j%mods)%mods*t%mods)%mods;
	  tx=(sum[2][m]-sum[2][m-j]+mods)%mods,
	    ty=(sum[0][m]-sum[0][m-j]+mods)%mods;
	  ret = (ret+(mods+tx-ty*j%mods)%mods*t%mods)%mods;
	}
      }
    }
    return ret;
  }
};

int main() {
  freopen("class.in", "r", stdin);
  freopen("class.out", "w", stdout);

  static Tree tree;
  int n, m;
  scanf("%d %d", &n, &m);
  tree.n = n;
  initC(m);
  initPow(maxn);
  for (int i=1; i<n; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    tree.addSide(u, v, w);
    tree.addSide(v, u, w);
  }

  printf("%lld", tree.build(1, 0, m));

  fclose(stdin);
  fclose(stdout);
  return 0;
}
